﻿
using System.Collections.Generic;
using agsXMPP;
using agsXMPP.protocol.client;
namespace sharpxbot.Core.Muc
{
    public class PresenceCache : IPresenceCache
    {
        public PresenceCache()
        {
            bareJids = new Dictionary<Jid, Jid>();
        }

        #region IPresenceCache Members

        public void ProcessPresence(Presence presence)
        {
            if (presence == null)
                return;

            if (presence.MucUser != null && presence.MucUser.Item.Jid != null)
                CachePresence(presence);
        }

        public Jid GetPrivateJid(Jid userGroupChatJid)
        {
            Jid result = null;
            bareJids.TryGetValue(userGroupChatJid, out result);

            return result;
        }

        #endregion

        private void CachePresence(Presence presence)
        {
            Jid mucUserJid = presence.MucUser.Item.Jid;

            switch (presence.Type)
            {
                case PresenceType.unavailable:
                    {
                        bareJids.Remove(presence.From);
                    }
                    break;
                case PresenceType.available:
                    {
                        bareJids[presence.From] = mucUserJid;
                    }
                    break;
            }
        }

        #region Private fields

        private Dictionary<Jid, Jid> bareJids;

        #endregion
    }
}
